package com.example.game.sdk.framework.plugin

import android.app.Activity
import android.app.Application
import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle
import android.view.KeyEvent
import com.example.game.sdk.framework.contract.ActivityLifecycleContract
import com.example.game.sdk.framework.contract.AppLifecycleContract
import com.example.game.sdk.framework.contract.InitializeContract
import org.json.JSONObject

internal abstract class DispatcherTemplate : AppLifecycleContract, ActivityLifecycleContract,
    InitializeContract {

    /**
     * 抽象属性，子类必须实现，返回一个插件列表，用于父类分发生命周期
     */
    protected abstract val lifecycleDispatchList: List<PluginTemplate>

    /**
     * 当前插件名称，当前key 标记当前插件
     */
    abstract val dispatcherName: String


    override fun onAppCreate(app: Application) {
        lifecycleDispatchList.forEach {
            it.onAppCreate(app)
        }
    }

    override fun init(params: JSONObject) {
        lifecycleDispatchList.forEach {
            it.init(params)
        }
    }

    override fun onCreate(activity: Activity, savedInstanceState: Bundle?) {
        lifecycleDispatchList.forEach {
            it.onCreate(activity, savedInstanceState)
        }
    }

    override fun onStart(activity: Activity) {
        lifecycleDispatchList.forEach {
            it.onStart(activity)
        }
    }

    override fun onResume(activity: Activity) {
        lifecycleDispatchList.forEach {
            it.onResume(activity)
        }
    }

    override fun onPause(activity: Activity) {
        lifecycleDispatchList.forEach {
            it.onPause(activity)
        }
    }

    override fun onStop(activity: Activity) {
        lifecycleDispatchList.forEach {
            it.onStop(activity)
        }
    }

    override fun onDestroy(activity: Activity) {
        lifecycleDispatchList.forEach {
            it.onDestroy(activity)
        }
    }

    override fun onRestart(activity: Activity) {
        lifecycleDispatchList.forEach {
            it.onRestart(activity)
        }
    }

    override fun onActivityResult(
        activity: Activity,
        requestCode: Int,
        resultCode: Int,
        data: Intent?
    ) {
        lifecycleDispatchList.forEach {
            it.onActivityResult(activity, requestCode, resultCode, data)
        }
    }

    override fun onConfigurationChanged(
        activity: Activity,
        newConfig: Configuration
    ) {
        lifecycleDispatchList.forEach {
            it.onConfigurationChanged(activity, newConfig)
        }
    }

    override fun onBackPressed(activity: Activity): Boolean {
        var isIntercepted = false
        lifecycleDispatchList.forEach { dispatcher ->
            val result = dispatcher.onBackPressed(activity)
            isIntercepted = isIntercepted || result
        }
        return isIntercepted
    }

    override fun onKeyDown(
        activity: Activity,
        keyCode: Int,
        event: KeyEvent?
    ): Boolean {
        var isIntercepted = false
        lifecycleDispatchList.forEach { dispatcher ->
            val result = dispatcher.onKeyDown(activity, keyCode, event)
            isIntercepted = isIntercepted || result
        }
        return isIntercepted
    }

    override fun onRequestPermissionsResult(
        activity: Activity,
        requestCode: Int,
        permissions: Array<out String?>,
        grantResults: IntArray
    ) {
        lifecycleDispatchList.forEach {
            it.onRequestPermissionsResult(activity, requestCode, permissions, grantResults)
        }
    }
}